package longrise.phone.com.bjjt_jyb.Activity;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

import kr.co.namee.permissiongen.PermissionFail;
import kr.co.namee.permissiongen.PermissionGen;
import longrise.phone.com.bjjt_jyb.R;
import longrise.phone.com.bjjt_jyb.Utils.RecycleBitmap;
import longrise.phone.com.bjjt_jyb.Utils.UiUtil;


public class GuidActivity extends Activity {

    ViewPager vp_guideviewPager;
    LinearLayout dot_layout;
    ArrayList<View> al = null;
    TextView textview_tomain = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.activity_guid);

        initUI();
        initData();
        initListener();
        //            6.0主动获取主要权限避免ANR
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestpermission();
        }

    }

    private void initUI() {
        vp_guideviewPager = (ViewPager) findViewById(R.id.vp_guideviewPager);
        dot_layout = (LinearLayout) findViewById(R.id.dot_layout);
        textview_tomain = (TextView) findViewById(R.id.textview_tomain);

    }

    private void initData() {

        al = new ArrayList<>();

        View view = View.inflate(this, R.layout.vp_guide_one, null);
        View view2 = View.inflate(this, R.layout.vp_guide_two, null);
        al.add(view);
        al.add(view2);

        initDots();// 初始化点

        vp_guideviewPager.setAdapter(new MyPagerAdapter());

        vp_guideviewPager.setCurrentItem(0);// 得到0
        updateIntroAndDot();
    }

    private void initListener() {
        vp_guideviewPager.setOnPageChangeListener(new OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                updateIntroAndDot();
                if (position == 1) {
                    textview_tomain.setVisibility(View.VISIBLE);
                } else {
                    textview_tomain.setVisibility(View.INVISIBLE);
                }
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
        });

        textview_tomain.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                Intent intent = new Intent();
                intent.setClass(GuidActivity.this, MainActivity.class);
                startActivity(intent);
                overridePendingTransition(R.anim.transition_to_anim, R.anim.transition_in_anim);
                finish();
            }
        });
    }

    /**
     * 初始化dot
     */
    private void initDots() {
        for (int i = 0; i < al.size(); i++) {
            View view = new View(this);
            LayoutParams params = new LayoutParams(UiUtil.dip2Px(8), UiUtil.dip2Px(8));
            if (i != 0) {
                params.leftMargin = 20;
            }
            view.setLayoutParams(params);
            view.setBackgroundResource(R.drawable.selector_dot);
            dot_layout.addView(view);
        }
    }

    /**
     * 更新点
     */
    private void updateIntroAndDot() {
        int currentPage = vp_guideviewPager.getCurrentItem();

        for (int i = 0; i < dot_layout.getChildCount(); i++) {
            dot_layout.getChildAt(i).setEnabled(i == currentPage);
        }
    }

    class MyPagerAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return al.size();
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {

            View imageView = al.get(position);

            container.addView(imageView);
            return imageView;
        }

    }

    @Override
    protected void onDestroy() {
        if (null != al) {
            for (View view : al) {
                if (view instanceof ViewGroup) {
                    RecycleBitmap.recycleViewGroup((ViewGroup) view);
                }
            }
            al.clear();
            al = null;
        }
        System.gc();
        super.onDestroy();
    }
    private void requestpermission() {
        PermissionGen.with(GuidActivity.this)
                .addRequestCode(400)
                .permissions(
                        Manifest.permission.CAMERA,
                        Manifest.permission.ACCESS_FINE_LOCATION,
                        Manifest.permission.ACCESS_COARSE_LOCATION,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.RECORD_AUDIO

                )
                .request();
    }
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        PermissionGen.onRequestPermissionsResult(this, requestCode, permissions, grantResults);
    }

    @PermissionFail(requestCode = 400)
    public void doFailSomething() {
        Toast.makeText(this, "请允许程序获得权限，提升流程效率", Toast.LENGTH_SHORT).show();
        requestpermission();

    }
}
